﻿using System;
using System.Data;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Web.Class;

namespace Web.Pages.QuShui.DiXiaS._7.ChaXunGL
{
    public partial class ZhiDingQJCX : BasePage
    {
        protected void btnExport_Click(object sender, EventArgs e)
        {
            ASPxGridViewExporter1.GridViewID = "gvShenJingXX";
            var sName = "指定年月区间查询信息";
            gvShenJingXX.Settings.ShowFooter = false;
            ASPxGridViewExporter1.WriteXlsToResponse(sName);
        }

        #region 属性

        /// <summary>
        ///     保存结果集1
        /// </summary>
        private DataTable dtShenJingXX
        {
            get { return ViewState["dtShenJingXX"] as DataTable; }
            set { ViewState["dtShenJingXX"] = value; }
        }

        private DataTable dtShenJingXX1
        {
            get { return ViewState["dtShenJingXX1"] as DataTable; }
            set { ViewState["dtShenJingXX1"] = value; }
        }

        private DataTable dtShenJingXX2
        {
            get { return ViewState["dtShenJingXX2"] as DataTable; }
            set { ViewState["dtShenJingXX2"] = value; }
        }

        private DataTable dtShenJingXX3
        {
            get { return ViewState["dtShenJingXX3"] as DataTable; }
            set { ViewState["dtShenJingXX3"] = value; }
        }

        #endregion

        #region 变量

        /// <summary>
        ///     数据访问操作类
        /// </summary>
        private readonly SpHelper _SpHelper = new SpHelper();

        /// <summary>
        ///     行政区
        /// </summary>
        private readonly QS_JX_XINGZHENGQBLL XZQ = new QS_JX_XINGZHENGQBLL();

        /// <summary>
        ///     行业分类
        /// </summary>
        private readonly QS_JX_HANGYEFLXXBLL HYFL = new QS_JX_HANGYEFLXXBLL();

        /// <summary>
        ///     取水用途
        /// </summary>
        private readonly QS_JX_QUSHUIXXBLL QSYT = new QS_JX_QUSHUIXXBLL();

        /// <summary>
        ///     管理部门
        /// </summary>
        private QS_JX_BUMENXXBLL GLBM = new QS_JX_BUMENXXBLL();

        /// <summary>
        ///     流域信息
        /// </summary>
        private QS_JX_LIUYUXXBLL LYXX = new QS_JX_LIUYUXXBLL();

        /// <summary>
        ///     水利分片
        /// </summary>
        private QS_JX_SHUILIFPXXBLL SLFP = new QS_JX_SHUILIFPXXBLL();

        /// <summary>
        ///     水资源分区
        /// </summary>
        private QS_JX_SHUIZIYFQBLL SZYFQ = new QS_JX_SHUIZIYFQBLL();

        /// <summary>
        ///     井标记
        /// </summary>
        private QS_JX_JINGBIAOJXXBLL JBJ = new QS_JX_JINGBIAOJXXBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack) InitData();
            LoadGridData();
        }


        /// <summary>
        ///     查询信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChaXun_Click(object sender, EventArgs e)
        {
            LoadGridData();
            if (dtShenJingXX == null || dtShenJingXX.Rows.Count < 1)
            {
                //txtYongHuH2.Text = "";
                //txtYongHuM2.Text = "";
                //txtHuanNeiHW.Text = "";
                //txJingBianH.Text = "";
                //txtDiZhi.Text = "";
                //txtXingZhengQ.Text = "";
                //txtJingLeiX.Text = "";
                //txtJingXingZ.Text = "";
                //txtJingBiaoJ.Text = "";
                //txtLiuYu.Text = "";
                //txtYeWuY.Text = "";
                //txtGuanLiBM.Text = "";
                //txtShuiZiYFQ.Text = "";
                //txtCengCi.Text = "";
                //txtShenDu.Text = "";
                //txtQuShuiYT.Text = "";
                //txtShuiLiFP.Text = "";
                //txtKouJing.Text = "";
                //txtHangYeFL.Text = "";
                //memoBeiZhu.Text = "";
                //txtYuanJingH.Text = "";
                //txtShuiBiaoSL.Text = "";
                //txtXuKeZ.Text = "";
                ////txtBenYueSL.Text = "";
                //zjl.Text = "共0条";
            }
        }


        /// <summary>
        ///     tab1导出excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            if (dtShenJingXX == null || dtShenJingXX.Rows.Count < 1)
                PageTools.AlertMsg(this, "没有符合的数据导出");
            else
                PageTools.ExportExcel(dtShenJingXX);
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            var wtype = BusinessTool.WType;
            gvShenJingXX.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //水量年月
            var water_y_m = BusinessTool.GetSLYMorZWNY(1, wtype).ToString();


            //加载行政区
            LoadXZQ();

            //加载取水用途
            LoadQSYT();
            //加载行业分类
            LoadHYFL();


            LoadGLBM();
        }


        /// <summary>
        ///     加载管理部门
        /// </summary>
        private void LoadGLBM()
        {
            for (var i = DateTime.Now.Year - 100; i <= DateTime.Now.Year + 100; i++)
            {
                cmbYear.Items.Add(i + "年", i.ToString());
                cmbYear1.Items.Add(i + "年", i.ToString());
            }

            for (var i = 1; i <= 12; i++)
            {
                cmbMonth.Items.Add(i + "月", i.ToString());
                cmbMonth1.Items.Add(i + "月", i.ToString());
            }

            cmbYear.Value = (BusinessTool.GetSLYMorZWNY(1, BusinessTool.WType) / 100).ToString();
            cmbYear1.Value = (BusinessTool.GetSLYMorZWNY(1, BusinessTool.WType) / 100).ToString();
            cmbMonth1.Value = (BusinessTool.GetSLYMorZWNY(1, BusinessTool.WType) % 100).ToString();

            cmbMonth.SelectedIndex = 0;
            cmbXingZhengQ.SelectedIndex = 0;
            cmbQuShuiYT.SelectedIndex = 0;
            cmbHuanNeiHW.SelectedIndex = 0;
            cmbHangYeLX.SelectedIndex = 0;
        }

        /// <summary>
        ///     加载行政区
        /// </summary>
        private void LoadXZQ()
        {
            var dt = XZQ.SelectQS_JX_XINGZHENGQsDynamic("I_JLZT=0", null);
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbXingZhengQ.Items.Add(dt.Rows[i]["S_XINGZHENGQM"].ToString(), dt.Rows[i]["S_XINGZHENGQH"]);
        }


        /// <summary>
        ///     加载 取水用途
        /// </summary>
        private void LoadQSYT()
        {
            var dt = QSYT.SelectQS_JX_QUSHUIXXesDynamic("I_JLZT=0", null);
            cmbQuShuiYT.Items.Insert(0, new ListEditItem("", ""));
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbQuShuiYT.Items.Add(dt.Rows[i]["S_QUSHUIMC"].ToString(), dt.Rows[i]["S_QUSHUIBH"]);
        }

        /// <summary>
        ///     加载行业分类
        /// </summary>
        private void LoadHYFL()
        {
            var dt = HYFL.SelectQS_JX_HANGYEFLXXesDynamic("I_JLZT=0", null);
            cmbHangYeLX.Items.Insert(0, new ListEditItem("", ""));
            for (var i = 0; i < dt.Rows.Count; i++)
                cmbHangYeLX.Items.Add(dt.Rows[i]["S_FENLEIMC"].ToString(), dt.Rows[i]["S_FENLEIBH"]);
        }


        /// <summary>
        ///     加载查询数据
        /// </summary>
        private void LoadGridData()
        {
            try
            {
                BindShenJingXX();
            }
            catch (Exception ex)
            {
            }
        }


        private void BindShenJingXX()
        {
            var ym = Convert.ToInt32(cmbYear.Value) * 100 + Convert.ToInt32(cmbMonth.Value);
            var ym1 = Convert.ToInt32(cmbYear1.Value) * 100 + Convert.ToInt32(cmbMonth1.Value);

            var yonghuh = txtYongHuH.Text.Trim();
            var yonghum = txtYongHuM.Text;
            var huanxian = cmbHuanNeiHW.Value == null ? "" : cmbHuanNeiHW.Value.ToString();
            var hangye = cmbHangYeLX.Value == null ? "" : cmbHangYeLX.Value.ToString();
            var xingzhengq = cmbXingZhengQ.Value == null ? "" : cmbXingZhengQ.Value.ToString();
            var qsyt = cmbQuShuiYT.SelectedIndex <= 0 ? null : cmbQuShuiYT.Value.ToString();

            var ds = _SpHelper.P_QS_CX_ZHIDINGQJCX(ym, ym1, yonghuh, yonghum, huanxian, hangye, xingzhengq, qsyt);

            dtShenJingXX = ds.Tables[0];
            gvShenJingXX.DataSource = ds.Tables[0];
            gvShenJingXX.DataBind();
            decimal hjCBSL = 0, hjKZSL = 0, hjKZJE = 0;
            for (int i = 0; i < dtShenJingXX.Rows.Count - 1; i++)
            {
                hjCBSL = hjCBSL + Convert.ToDecimal(dtShenJingXX.Rows[i]["抄表水量"]);
                hjKZSL = hjKZSL + Convert.ToDecimal(dtShenJingXX.Rows[i]["开账水量"]);
                hjKZJE = hjKZJE + Convert.ToDecimal(dtShenJingXX.Rows[i]["开账金额"]);
            }

            zjl.Text = "共" + gvShenJingXX.VisibleRowCount + "条,合计开账水量:" + hjKZSL + ",开账金额:" + hjKZJE.ToString("0.00") +
                       "元";
            gvShenJingXX.FocusedRowIndex = -1;
            gvShenJingXX.FocusedRowIndex = 0;
        }

        /// <summary>
        ///     前台表格行改变事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, CallbackEventArgs e)
        {
            var id = Convert.ToInt32(hdCollection.Get("ID"));
            if (e.Parameter == "setinfo")
            {
                var msg = "";
                if (dtShenJingXX != null)
                {
                    var y = Convert.ToInt32(cmbYear.Value);
                    var m = Convert.ToInt32(cmbMonth.Value);
                    var where = "";

                    where = " and T.ID='" + id + "'";

                    var dt_temp = _SpHelper.P_QS_CX_SHENJINGXXCX(BusinessTool.WType, y, m, where).Tables[0];

                    msg = string.Join("|",
                        dt_temp.Rows[0]["S_YONGHUH"].ToString(),
                        dt_temp.Rows[0]["S_YONGHUM"].ToString(),
                        dt_temp.Rows[0]["HNHW"].ToString(),
                        dt_temp.Rows[0]["S_JINGBH"].ToString(),
                        dt_temp.Rows[0]["S_DIZHI"].ToString(),
                        dt_temp.Rows[0]["S_XINGZHENGQM"].ToString(),
                        dt_temp.Rows[0]["S_JINGLEIXBH"].ToString(),
                        dt_temp.Rows[0]["S_JINGZHUANGTBH"].ToString(),
                        dt_temp.Rows[0]["S_JINGBIAOSBH"].ToString(),
                        dt_temp.Rows[0]["S_LIUYUMC"].ToString(),
                        dt_temp.Rows[0]["S_YEWUY"].ToString(),
                        dt_temp.Rows[0]["S_BUMENMC"].ToString(),
                        dt_temp.Rows[0]["S_SHUIZIYMC"].ToString(),
                        dt_temp.Rows[0]["I_CENG"].ToString(),
                        dt_temp.Rows[0]["S_SHENGDU"].ToString(),
                        dt_temp.Rows[0]["S_QUSHUIMC"].ToString(),
                        dt_temp.Rows[0]["S_SHUILIFPMC"].ToString(),
                        dt_temp.Rows[0]["I_KONGJING"].ToString(),
                        dt_temp.Rows[0]["S_FENLEIMC"].ToString(),
                        dt_temp.Rows[0]["S_BEIZHU"].ToString(),
                        dt_temp.Rows[0]["S_LAOBIANH"].ToString(),
                        dt_temp.Rows[0]["BS"].ToString(),
                        dt_temp.Rows[0]["S_XUKEZHENGBH"].ToString());
                }

                e.Result = e.Parameter + "|0|" + msg;
            }
        }

        #endregion
    }
}